﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Site.Models;
using Bricks.Business.Service;
using Bricks.Business.Models.ProjectContract;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Models.Systems;
using Bricks.Business.Data.Repositories.Systems;
using Bricks.Business.Data.Repositories.Account;
using Bricks.Business.Models.Account;
using Bricks.Business.Data.Repositories.QualityManagement;
using Bricks.Business.Data.Repositories.SafetyManagement;
using Bricks.Business.Models.QualityManagement;
using Bricks.Business.Models.SafetyManagement;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(ISystemProcessMessageService))]
    internal class SystemProcessMessageService : ISystemProcessMessageService
    {
        [Import]
        protected ISystemProcessMessageRepository SystemProcessMessageRepository { get; set; }
        [Import]
        protected IUserInfoRepository UserInfoRepository { get; set; }
        [Import]
        protected IQualityCircularRepository QualityCircularRepository { get; set; }
        [Import]
        protected ICircularRepository CircularRepository { get; set; }
        [Import]
        protected IContractInfoRepository ContractInfoRepository { get; set; }
        [Import]
        protected ICommonContractInfoRepository CommonContractInfoRepository { get; set; }


        public List<SystemProcessMessageView> GetSystemProcessMessage(string userName)
        {
            List<SystemProcessMessageView> lstSystemProcessMessageView = new List<SystemProcessMessageView>();
            IQueryable<SystemProcessMessage> systemProcessMessages = SystemProcessMessageRepository.Entities.Where(s => s.ProcessUser == userName);
            foreach (SystemProcessMessage systemProcessMessage in systemProcessMessages)
            {
                SystemProcessMessageView view = new SystemProcessMessageView();
                view.ID = systemProcessMessage.ID;
                view.MessageType = systemProcessMessage.MessageType;
                view.RecordID = systemProcessMessage.RecordID;

                string description = string.Empty;
                UserInfo userInfo = UserInfoRepository.Entities.FirstOrDefault(c => c.UserName == systemProcessMessage.SendUser);
                if (userInfo != null)
                {
                    description += "【" + userInfo.RealName + "】在编辑";
                }
                switch (systemProcessMessage.MessageType)
                {
                    case "QualityCircular":
                        QualityCircular qualityCircular = QualityCircularRepository.Entities.FirstOrDefault(q => q.ID == systemProcessMessage.RecordID);
                        if (qualityCircular != null)
                        {
                            description += "质量通报【" + qualityCircular.CircularName + "】的时候";
                            SetDescriptionForCircular(systemProcessMessage.RefType, systemProcessMessage.RefID, ref description);
                        }
                        break;
                    case "SafetyCircular":
                        Circular circular = CircularRepository.Entities.FirstOrDefault(q => q.ID == systemProcessMessage.RecordID);
                        if (circular != null)
                        {
                            description += "安全通报【" + circular.CircularName + "】的时候";
                            SetDescriptionForCircular(systemProcessMessage.RefType, systemProcessMessage.RefID, ref description);
                        }
                        break;
                }
                view.Description = description;
                view.SendTimeStr = systemProcessMessage.SendTime.ToString("yyyy-MM-dd HH:mm");
                lstSystemProcessMessageView.Add(view);
            }
            return lstSystemProcessMessageView;
        }

        private void SetDescriptionForCircular(string refType, Guid refID, ref string description)
        {
            if (refType == "Contract")
            {
                ContractInfo contractInfo = ContractInfoRepository.Entities.FirstOrDefault(c => c.ID == refID);
                if (contractInfo != null)
                {
                    description += "选择了合同【" + contractInfo.ContractName + "】";
                }
            }
            else
            {
                CommonContractInfo commonContractInfo = CommonContractInfoRepository.Entities.FirstOrDefault(c => c.ID == refID);
                if (commonContractInfo != null)
                {
                    description += "选择了合同【" + commonContractInfo.ContractName + "】";
                }
            }
        }

        public void SaveSystemProcessMessage(List<SystemProcessMessageView> saveList)
        {
            if (saveList.Count() > 0)
            {
                DeleteSystemProcessMessageByRecordID(saveList[0].RecordID);
                foreach (SystemProcessMessageView systemProcessMessageView in saveList)
                {
                    SystemProcessMessage systemProcessMessage = new SystemProcessMessage();
                    systemProcessMessage.ID = systemProcessMessageView.ID;
                    systemProcessMessage.MessageType = systemProcessMessageView.MessageType;
                    systemProcessMessage.RecordID = systemProcessMessageView.RecordID;
                    systemProcessMessage.RefType = systemProcessMessageView.RefType;
                    systemProcessMessage.RefID = systemProcessMessageView.RefID;
                    systemProcessMessage.ProcessUser = systemProcessMessageView.ProcessUser;
                    systemProcessMessage.SendUser = systemProcessMessageView.SendUser;
                    systemProcessMessage.SendTime = systemProcessMessageView.SendTime;
                    SystemProcessMessageRepository.Insert(systemProcessMessage);
                }
            }
        }

        private void DeleteSystemProcessMessageByRecordID(Guid recordID)
        {
            List<Guid> lstDeleteID = new List<Guid>();
            IQueryable<SystemProcessMessage> systemProcessMessages = SystemProcessMessageRepository.Entities.Where(s => s.RecordID == recordID);
            foreach (SystemProcessMessage systemProcessMessage in systemProcessMessages)
            {
                lstDeleteID.Add(systemProcessMessage.ID);
            }
            foreach (Guid id in lstDeleteID)
            {
                SystemProcessMessageRepository.Delete(id);
            }
        }

        public string DeleteSystemProcessMessage(Guid id)
        {
            try
            {
                PublicHelper.CheckArgument(id, "id");
                SystemProcessMessage record = SystemProcessMessageRepository.Entities.FirstOrDefault(s => s.ID == id);
                if (record != null)
                {
                    SystemProcessMessageRepository.Delete(record);
                }
                return "Success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}